// pages/pulishFullJob/pulishFullJob.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: '',
    formlist: [
      { isrequire: true, label: '公司名称', key: 'inCompany', valueType: 'text',tips: '请填写', type: 'input' },
      { isrequire: true, label: '职位名称', key: 'name', valueType: 'text',tips: '请填写', type: 'input',},
      { isrequire: true, label: '工作地点', key: 'workPlace', valueType: 'text', tips: '请选择', type: 'select', mode:'region'},
      { isrequire: true, label: '薪资范围', key: 'salary', index: [0, 0],valueType: 'text', tips: '请选择，单位K', type: 'select', mode: 'multiSelector' },
      { isrequire: true, label: '经验要求', key: 'requireYears', index: [0, 0], valueType: 'text', tips: '请选择，单位年', type: 'select', mode: 'multiSelector'  },
      { isrequire: true, label: '学历要求', key: 'requireSchool', valueType: 'text', tips: '请选择', type: 'select', data: ['博士后', '博士 ', '硕士', '本科', '专科', '高中', '高中以下']},
      { isrequire: true, label: '职位描述', key: 'positionDescrip', valueType: 'text', tips: '请填写', type: 'input', url: "/pages/jobDescrip/jobDescrip?page=4" }
    ],
    formData: {
      inCompany: '',
      name: "",
      workPlace: '',
      salary: '',
      requireYears: '',
      requireSchool: '',
      positionDescrip: '',
      property: '全职',
    },
    showData: {
      positionDescrip:'已填写'
    },
    canSubmit: false,
    isEdit:false
  },
  bindPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    if (item.data) {
      this.data.formData[item.key] = item.data[e.detail.value]
    } else {
      this.data.formData[item.key] = e.detail.value
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindMultiPickerChange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    this.data.formlist[index].index = e.detail.value;
    if (item.key=='salary'){
      this.data.formData[item.key] = item.data[0][e.detail.value[0]] + 'K-' + item.data[1][e.detail.value[1]] + 'K';
    }else{
      this.data.formData[item.key] = item.data[0][e.detail.value[0]] + '-' + item.data[1][e.detail.value[1]] + '年';
    }
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  bindColumnchange: function (e) {
    var item = e.currentTarget.dataset.item;
    var index = e.currentTarget.dataset.index;
    if (e.detail.column == 0) {
      var arr = [];
      arr = arr.concat(item.data[0].slice(e.detail.value, item.data[0].length));
      this.setData({
        ['formlist[' + index + '].data[1]']: arr
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
      if (options.isEdit) {
        this.setData({
          isEdit: true
        })
        wx.getStorage({
          key: 'infoItem',
          success: function (res) {
            for (var j in that.data.formData) {
              if (res.data[j] !== '' && res.data[j] != undefined && res.data[j] != null) {
                if ((j == 'workPlace') && (typeof res.data[j]=='string')) {
                  that.data.formData[j] = res.data[j].split(',');
                } else {
                  that.data.formData[j] = res.data[j];
                }
              }
            }
            that.data.formData.id = res.data.id;
            that.setData({
              formData: that.data.formData
            })
            that.getMonthIncome();
            that.getRequireYears();
            that.setSelectValue('requireSchool', 5);
            that.changeValue();
            wx.removeStorage({
              key: 'infoItem'
            })
          }
        })
      } else {
        this.getUserIdentity();
        this.getMonthIncome();
        this.getRequireYears();
        this.setSelectValue('requireSchool', 5);
      }
  },
  getUserIdentity: function () {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'recruitPersonalInfo/getOne', //仅为示例，并非真实的接口地址
      data: { userId: this.data.userInfo.id },
      success: function (res) {
        if (res.data.success) {
          if (res.data.data) {
            that.data.formData.inCompany= res.data.data.onCompanyName;
            that.setData({
              formData: that.data.formData
            })
          }
        }
      }
    })
  },
  setSelectValue: function (key, index) {
    if (this.data.formData[key] !== '') {
      var that = this;
      this.data.formlist[index].data.forEach(function (ele, j) {
        if (ele == that.data.formData[key]) {
          that.setData({
            ['formlist[' + index + '].index']: j,
            ['showData.' + key]: ele
          })
        }
      })
    }
  },
  inputTap: function (e) {
    wx.navigateTo({
      url: e.currentTarget.dataset.url + '&value=' + this.data.formData.positionDescrip
    })
  },
  getMonthIncome: function () {
    var arr = [];
    for (var i = 1; i <= 50; i++) {
      arr.push(i);
    }
    arr.push('50k以上');
    if (this.data.formData.salary != '') {
      if (this.data.formData.salary.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.salary.split('-');
        var heightiIndex = [0, 0];
        arr.forEach(function (ele, index) {
          if (ele == monthIncome[0].substring(0, monthIncome[0].length - 1)) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1].substring(0, monthIncome[1].length - 1)) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[3].index']: heightiIndex,
        })
      }
    }
    this.setData({
      ['formlist[3].data']: [arr, arr]
    })
  },
  getRequireYears: function () {
    var arr = [];
    for (var i = 1; i <= 20; i++) {
      arr.push(i);
    }
    arr.push('20年以上');
    if (this.data.formData.requireYears != '') {
      if (this.data.formData.requireYears.indexOf('-') >= 0) {
        var monthIncome = this.data.formData.requireYears.split('-');
        var heightiIndex = [0, 0];
        arr.forEach(function (ele, index) {
          if (ele == monthIncome[0]) {
            heightiIndex[0] = index;
          }
          if (ele == monthIncome[1].substring(0, monthIncome[1].length - 1)) {
            heightiIndex[1] = index;
          }
        })
        this.setData({
          ['formlist[4].index']: heightiIndex,
        })
      }
    }
    this.setData({
      ['formlist[4].data']: [arr, arr]
    })
  },
  changeTextarea: function (e) {//输入框值改变事件
    var key = e.target.dataset.value;
    this.data.formData[key] = e.detail.value;
    this.setData({
      formData: this.data.formData
    })
    this.changeValue();
  },
  del: function (e) {//删除
    if (this.data.formData.id) {
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + 'recruitPositionInfo/del', //仅为示例，并非真实的接口地址
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: { id: this.data.formData.id},
        success: function (res) {
          if (res.data.success) {
            wx.navigateBack({
              delta: 1
            })
            // wx.redirectTo({
            //   url: '/pages/myRecruit/myRecruit?page=1',
            // })
          } else {
            wx.showModal({
              title: '提示',
              content: '删除失败',
              showCancel:false
            })
          }
        }
      })
    }
  },
  saveInfo: function (e) {//新增修改保存
    if (this.data.canSubmit) {
      var param = {};
      var url = '';
      if (this.data.isEdit) {
        url = 'recruitPositionInfo/update';
      } else {
        url = 'recruitPositionInfo/save';
        param.userId = this.data.userInfo.id;
      }
      for (var j in this.data.formData) {
        param[j] = this.data.formData[j];
      }
      if (param.workPlace != '') {
        if (param.workPlace.indexOf(',') < 0) {
          param.workPlace = param.workPlace.join(',');
        }
      }
      var that = this;
      wx.request({
        url: app.globalData.httpApiCar + url, //仅为示例，并非真实的接口地址
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: param,
        success: function (res) {
          if (res.data.success) {
            if (that.data.isEdit) {
              wx.navigateBack({
                delta: 1
              })
            } else {
              wx.setStorage({
                key: 'url',
                data: '/pages/recruitHome/recruitHome',
              })
              wx.redirectTo({
                url: '/pages/regsuccess/regsuccess?btnText=返回首页&showText=发布成功'
              })
            }
          }else{
            wx.showModal({
              title: '提示',
              content: res.data.error,
              showCancel: false
            })
          }
        }
      })
    }
  },
  changeValue() {
    var flag = true;
    var item = this.data.formData;
    if ((item.inCompany == '') || (item.name == '') || (item.workPlace == '') || (item.salary == '') || (item.requireYears == '') || (item.requireSchool == '') || (item.positionDescrip == '') || (item.property == '')) {
      flag = false;
    }
    this.setData({
      canSubmit: flag
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.getStorage({
      key: 'descripton',
      success: function (res) {
        that.setData({
          'formData.positionDescrip': res.data
        })
        that.changeValue();
        wx.removeStorage({
          key: 'descripton',
          success: function (res) { },
        })
      },
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})